package com.itrip.itripauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrip.itripauth.entity.Comment;
import com.itrip.itripauth.entity.Hotel;
import com.itrip.itripauth.entity.LabelDic;
import com.itrip.itripauth.mapper.CommentMapper;
import com.itrip.itripauth.service.CommentService;
import com.itrip.itripauth.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 四阶段项目二组
 * @since 2020-02-25
 */
@Service
@Transactional
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Hotel getHotelDesc(Integer hotelId) {
        return commentMapper.getHotelDesc(hotelId);
    }

    @Override
    public List<LabelDic> getTravelType() {
        return commentMapper.getTravelType();
    }

    @Override
    public int addComment(CommentVo commentVo) {

        //ImgVo imgVo = new ImgVo();
        //设置图片类型为评论
        commentVo.setImgType(2);
        Integer avgScore = (commentVo.getFacilitiesScore()+commentVo.getServiceScore()+commentVo.getHygieneScore()+commentVo.getPositionScore())/4;
        commentVo.setScore(avgScore);
        int count = commentMapper.addComment(commentVo);
        if (count>0) {
            Comment comment = commentMapper.getComment(commentVo.getOrderId());
            commentVo.setCommentId(comment.getId());
            for (int i = 0; i < commentVo.getItripImages().size(); i++) {
                String image = "http://img.itrip.test.com/comment/";
                String topName = "{imgUrl=";
                String endName = "}";
                String fileUrl = commentVo.getItripImages().get(i).toString();
                String url = fileUrl.replace(topName, "");
                String fileName = url.replace(endName, "");
                commentVo.setImgUrl(image + fileName);
                commentVo.setPosition(i + 1);
                commentMapper.addImage(commentVo);
            }
            //修改订单状态
            commentMapper.updateOrder(commentVo.getOrderId());
        }
        return count;
    }

    @Override
    public HotelScoreVo scoreList(Integer hotelId) {
        return commentMapper.scoreList(hotelId);
    }


    @Override
    public CommentCountVo commCount(Integer hotelId) {
        //根据ld获取所有评论
        QueryWrapper qw=new QueryWrapper();
        qw.eq("hotel_id",hotelId);
        List<Comment> commentList=commentMapper.selectList(qw);
        CommentCountVo count=new CommentCountVo();
        for (Comment comment:commentList) {
            count.setAllcomment(count.getAllcomment()+1);
            if(comment.getIsHavingImg()==1){
                count.setHavingimg(count.getHavingimg()+1);
            }
            if (comment.getIsOk()==1){
                count.setIsok(count.getIsok()+1);
            }else{
                count.setImprove(count.getImprove()+1);
            }
        }
        return count;
    }

    @Override
    public PageVo<Comment> getCommentList(CommentListVo commentListVo) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("hotel_id",commentListVo.getHotelId());
        if (commentListVo.getIsOk()==1){
            queryWrapper.eq("is_ok",1);
        }else if (commentListVo.getIsOk()==0){
            queryWrapper.eq("is_ok",0);
        }
        if (commentListVo.getIsHavingImg()==1){
            queryWrapper.eq("is_having_img",1);
        }
        Page<Comment> page = new Page<Comment>(commentListVo.getPageNo(),commentListVo.getPageSize());
        IPage<Comment> orderF= commentMapper.selectPage(page,queryWrapper);
        PageVo<Comment> pageVo=new PageVo<Comment>(commentListVo.getPageNo(),commentListVo.getPageSize(), Math.toIntExact(orderF.getTotal()));
        pageVo.setRows(orderF.getRecords());
        return pageVo;
    }
}
